﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using CNav.Business.Services;
using CNav.Common;
using CNav.Domain;
using CNav.Model;
using CNav.Support;
using log4net;

namespace CNav.Controllers
{
    [Authorize]
    public class RegisterBusinessController : BaseController
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(RegisterBusinessController));

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet, AllowAnonymous]
        public ActionResult Register()
        {
            var model = new RegisterBusinessModel();

            return View(model);
        }

        [HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
        public ActionResult Register(RegisterBusinessModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var business = new RegisterBusiness
                    {
                        Email = model.Email,
                        BusinessName = model.BusinessName,
                        Password = model.Password,
                        PhoneNumber = model.PhoneNumber,
                        FaxNumber = model.FaxNumber
                    };
                    var response = Service.Get<IBusinessAccountService>().Register(business);

                    if (response.Data != 0) //TODO: Send activation email
                        return RedirectToAction("Index", "Home");

                    foreach (string e in response.Error)
                    {
                        ModelState.AddModelError("", e);
                    }
                }
                catch (Exception e)
                {
                    e.ToString();
                    ModelState.AddModelError("", "Error registring business.");
                }
            }

            return View(model);
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var generalService = Service.Get<IGeneralService>();
            var businessAccountService = Service.Get<IBusinessAccountService>();
            var addressService = Service.Get<IAddressService>();

            var countryList = generalService.GetCountries();
            var business = businessAccountService.GetBusiness(id);

            if (business.Data != null)
            {
                var businessModel = Mapper.Map<UpdateBusinessModel>(business.Data);

                var address = addressService.GetAddress(businessModel.AddressID);

                businessModel.Address = (address.Data == null || address.Data.ID == 1 )
                    ? new AddressModel()
                    : Mapper.Map<AddressModel>(address.Data);

                businessModel.Address.CountryList = countryList;

                return View(businessModel);
            }

            //"Business details provided does not exist, Please contact your system administrator."
            return RedirectToAction("Details", "UserProfile", new { id = id });
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Edit(UpdateBusinessModel model, AddressModel addressModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var business = Mapper.Map<Domain.Business>(model);

                    business.Address = Mapper.Map<Address>(addressModel);

                    ResponseData<Domain.Business> updateResponseData = Service.Get<IBusinessAccountService>().UpdateBusiness(business);

                    if (updateResponseData.Validate())
                    {
                        Session[Constant.SessionUser] = null;
                        return RedirectToAction("Details", "UserProfile", new {id = model.ID});
                    }

                    foreach (var error in updateResponseData.Error)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
                catch (Exception e)
                {
                    e.ToString();
                    //TODO:: Handle error
                }
            }

            var generalService = Service.Get<IGeneralService>();
     
            var countryList = generalService.GetCountries();
         
            model.Address = addressModel;
            model.Address.CountryList = countryList;
          
            return View(model);
        }
    }
}